#pragma once

#include <iostream>
#include <string>
#include <fstream>
#include <unordered_map>
#include "Log.hpp"
#include "Common.hpp"
using namespace LogMoudule;
const std::string gdictname = "dict.txt";
const std::string gpath = "./";
const std::string gsep = ": ";
class Dict
{
private:
    bool LoadDictionary()
    {
        std::string file = _path + _filename;
        std::ifstream in(file.c_str());
        if (!in.is_open())
        {
            LOG(LogLevel::ERROR) << "open file " << file << " error";
            return false;
        }
        std::string line;
        while (getline(in, line))
        {
            std::string key;
            std::string value;
            SplitString(line, &key, &value, gsep);
            _dictionary.insert({key, value});
        }
        in.close();
        return true;
    }
public:
    Dict(const std::string &path = gpath, const std::string &filename = gdictname)
        : _path(path), _filename(filename)
    {
        if(!LoadDictionary())
        {
            LOG(LogLevel::ERROR)<<"Load error!";
        }else LOG(LogLevel::INFO)<<"Load success!";
    }

    // banana: 香蕉
    std::string Translate(const std::string &word)
    {
        auto is_find = _dictionary.find(word);
        if (is_find == _dictionary.end())
            return "None";

        return _dictionary[word];
    }
    ~Dict()
    {
    }

private:
    std::unordered_map<std::string, std::string> _dictionary;
    std::string _path;
    std::string _filename;
};
